<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['specialEquipmentAccount:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      actionWidth="300"
      :rowClassName="rowClassName"
    >
      <template #actionButton="{ row }">
        <el-button
          type="primary"
          v-if="row.fileUrl"
          @click="handleDownLoad(row.fileUrl)"
          >证照</el-button
        >
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['specialEquipmentAccount:edit']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['specialEquipmentAccount:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :slots="['fileSlot']"
    >
      <template #fileSlot>
        <el-form-item label="文件上传" prop="fileId">
          <FileUpload
            :limit="1"
            :fileSize="30"
            :fileType="['png', 'jpeg', 'jpg']"
            v-model="files"
          ></FileUpload>
        </el-form-item>
      </template>
    </DialogVform>
    <!-- <selectZzVue
      type="SECURITY_VALVE_ACCOUNT"
      ref="selectZz"
      :done="getList"
    ></selectZzVue> -->
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/aqsc/tzsbtz.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
// import ImportExcel from "@/components/ImportExcel/index.vue";
// import ExportExcel from "@/components/ExportExcel/index.vue";
// import selectZzVue from "../components/selectZz.vue";

const { proxy } = getCurrentInstance();

const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    [
      "registrationCode",
      "equipmentType",
      "equipmentClass",
      "equipmentBreed",
      "specifications",
    ].includes(i.prop)
  ),
});
const showSearch = ref(true);
const dialogVform = ref();
const table = ref();
const form = ref({});
// 补充修改.新增事件
const plugEdit = (row) => {
  form.value.fileName = row.fileName;
  form.value.fileId = row.fileId;
  form.value.fileUrl = row.fileUrl;

  if (row.fileId) {
    files.value = [
      {
        name: row.fileName,
        id: row.fileId,
        url: row.fileUrl,
      },
    ];
  }
};
const plugAdd = () => {
  files.value = [];
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  handleDownLoad,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  plugAdd,
  plugEdit,
});

const files = ref([]);
watch(
  () => files,
  (newData) => {
    if (newData.value.length === 0) {
      form.value.fileName = "";
      form.value.fileId = "";
      form.value.fileUrl = "";
    } else {
      form.value.fileName = newData.value[0].name;
      form.value.fileId = newData.value[0].id;
      form.value.fileUrl = newData.value[0].url;
    }
  },
  {
    deep: true,
  }
);

const rowClassName = ({ row }) => {
  const nowTime = Date.now();
  const endTime = new Date(row.nextCheckDate).getTime();
  if (endTime && nowTime >= endTime) {
    return "yellow";
  } else {
    return "";
  }
};
</script>

<style lang="scss" scoped></style>
